{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 0. Install and Import Dependencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: mediapipe in c:\\programdata\\anaconda3\\lib\\site-packages (0.8.5)\n",
      "Requirement already satisfied: opencv-python in c:\\programdata\\anaconda3\\lib\\site-packages (4.2.0.34)\n",
      "Requirement already satisfied: pandas in c:\\programdata\\anaconda3\\lib\\site-packages (0.25.1)\n",
      "Requirement already satisfied: scikit-learn in c:\\programdata\\anaconda3\\lib\\site-packages (0.21.3)\n",
      "Requirement already satisfied: opencv-contrib-python in c:\\programdata\\anaconda3\\lib\\site-packages (from mediapipe) (4.2.0.34)\n",
      "Requirement already satisfied: wheel in c:\\programdata\\anaconda3\\lib\\site-packages (from mediapipe) (0.33.6)\n",
      "Requirement already satisfied: absl-py in c:\\programdata\\anaconda3\\lib\\site-packages (from mediapipe) (0.12.0)\n",
      "Requirement already satisfied: attrs>=19.1.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from mediapipe) (19.2.0)\n",
      "Requirement already satisfied: numpy in c:\\programdata\\anaconda3\\lib\\site-packages (from mediapipe) (1.16.5)\n",
      "Requirement already satisfied: six in c:\\programdata\\anaconda3\\lib\\site-packages (from mediapipe) (1.12.0)\n",
      "Requirement already satisfied: protobuf>=3.11.4 in c:\\programdata\\anaconda3\\lib\\site-packages (from mediapipe) (3.15.8)\n",
      "Requirement already satisfied: python-dateutil>=2.6.1 in c:\\programdata\\anaconda3\\lib\\site-packages (from pandas) (2.8.0)\n",
      "Requirement already satisfied: pytz>=2017.2 in c:\\programdata\\anaconda3\\lib\\site-packages (from pandas) (2019.3)\n",
      "Requirement already satisfied: joblib>=0.11 in c:\\programdata\\anaconda3\\lib\\site-packages (from scikit-learn) (0.13.2)\n",
      "Requirement already satisfied: scipy>=0.17.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from scikit-learn) (1.3.1)\n"
     ]
    }
   ],
   "source": [
    "!pip install mediapipe opencv-python pandas scikit-learn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import mediapipe as mp # Import mediapipe\n",
    "import cv2 # Import opencv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "mp_drawing = mp.solutions.drawing_utils # Drawing helpers\n",
    "mp_holistic = mp.solutions.holistic # Mediapipe Solutions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. Make Some Detections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "cap = cv2.VideoCapture(0)\n",
    "# Initiate holistic model\n",
    "with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:\n",
    "    \n",
    "    while cap.isOpened():\n",
    "        ret, frame = cap.read()\n",
    "        \n",
    "        # Recolor Feed\n",
    "        image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n",
    "        image.flags.writeable = False        \n",
    "        \n",
    "        # Make Detections\n",
    "        results = holistic.process(image)\n",
    "        # print(results.face_landmarks)\n",
    "        \n",
    "        # face_landmarks, pose_landmarks, left_hand_landmarks, right_hand_landmarks\n",
    "        \n",
    "        # Recolor image back to BGR for rendering\n",
    "        image.flags.writeable = True   \n",
    "        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\n",
    "        \n",
    "        # 1. Draw face landmarks\n",
    "        mp_drawing.draw_landmarks(image, results.face_landmarks, mp_holistic.FACE_CONNECTIONS, \n",
    "                                 mp_drawing.DrawingSpec(color=(80,110,10), thickness=1, circle_radius=1),\n",
    "                                 mp_drawing.DrawingSpec(color=(80,256,121), thickness=1, circle_radius=1)\n",
    "                                 )\n",
    "        \n",
    "        # 2. Right hand\n",
    "        mp_drawing.draw_landmarks(image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS, \n",
    "                                 mp_drawing.DrawingSpec(color=(80,22,10), thickness=2, circle_radius=4),\n",
    "                                 mp_drawing.DrawingSpec(color=(80,44,121), thickness=2, circle_radius=2)\n",
    "                                 )\n",
    "\n",
    "        # 3. Left Hand\n",
    "        mp_drawing.draw_landmarks(image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS, \n",
    "                                 mp_drawing.DrawingSpec(color=(121,22,76), thickness=2, circle_radius=4),\n",
    "                                 mp_drawing.DrawingSpec(color=(121,44,250), thickness=2, circle_radius=2)\n",
    "                                 )\n",
    "\n",
    "        # 4. Pose Detections\n",
    "        mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_holistic.POSE_CONNECTIONS, \n",
    "                                 mp_drawing.DrawingSpec(color=(245,117,66), thickness=2, circle_radius=4),\n",
    "                                 mp_drawing.DrawingSpec(color=(245,66,230), thickness=2, circle_radius=2)\n",
    "                                 )\n",
    "                        \n",
    "        cv2.imshow('Raw Webcam Feed', image)\n",
    "\n",
    "        if cv2.waitKey(10) & 0xFF == ord('q'):\n",
    "            break\n",
    "\n",
    "cap.release()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results.face_landmarks.landmark[0].visibility"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. Capture Landmarks & Export to CSV\n",
    "<!--<img src=\"https://i.imgur.com/8bForKY.png\">-->\n",
    "<!--<img src=\"https://i.imgur.com/AzKNp7A.png\">-->"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "import os\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "501"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num_coords = len(results.pose_landmarks.landmark)+len(results.face_landmarks.landmark)\n",
    "num_coords"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "landmarks = ['class']\n",
    "for val in range(1, num_coords+1):\n",
    "    landmarks += ['x{}'.format(val), 'y{}'.format(val), 'z{}'.format(val), 'v{}'.format(val)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['class',\n",
       " 'x1',\n",
       " 'y1',\n",
       " 'z1',\n",
       " 'v1',\n",
       " 'x2',\n",
       " 'y2',\n",
       " 'z2',\n",
       " 'v2',\n",
       " 'x3',\n",
       " 'y3',\n",
       " 'z3',\n",
       " 'v3',\n",
       " 'x4',\n",
       " 'y4',\n",
       " 'z4',\n",
       " 'v4',\n",
       " 'x5',\n",
       " 'y5',\n",
       " 'z5',\n",
       " 'v5',\n",
       " 'x6',\n",
       " 'y6',\n",
       " 'z6',\n",
       " 'v6',\n",
       " 'x7',\n",
       " 'y7',\n",
       " 'z7',\n",
       " 'v7',\n",
       " 'x8',\n",
       " 'y8',\n",
       " 'z8',\n",
       " 'v8',\n",
       " 'x9',\n",
       " 'y9',\n",
       " 'z9',\n",
       " 'v9',\n",
       " 'x10',\n",
       " 'y10',\n",
       " 'z10',\n",
       " 'v10',\n",
       " 'x11',\n",
       " 'y11',\n",
       " 'z11',\n",
       " 'v11',\n",
       " 'x12',\n",
       " 'y12',\n",
       " 'z12',\n",
       " 'v12',\n",
       " 'x13',\n",
       " 'y13',\n",
       " 'z13',\n",
       " 'v13',\n",
       " 'x14',\n",
       " 'y14',\n",
       " 'z14',\n",
       " 'v14',\n",
       " 'x15',\n",
       " 'y15',\n",
       " 'z15',\n",
       " 'v15',\n",
       " 'x16',\n",
       " 'y16',\n",
       " 'z16',\n",
       " 'v16',\n",
       " 'x17',\n",
       " 'y17',\n",
       " 'z17',\n",
       " 'v17',\n",
       " 'x18',\n",
       " 'y18',\n",
       " 'z18',\n",
       " 'v18',\n",
       " 'x19',\n",
       " 'y19',\n",
       " 'z19',\n",
       " 'v19',\n",
       " 'x20',\n",
       " 'y20',\n",
       " 'z20',\n",
       " 'v20',\n",
       " 'x21',\n",
       " 'y21',\n",
       " 'z21',\n",
       " 'v21',\n",
       " 'x22',\n",
       " 'y22',\n",
       " 'z22',\n",
       " 'v22',\n",
       " 'x23',\n",
       " 'y23',\n",
       " 'z23',\n",
       " 'v23',\n",
       " 'x24',\n",
       " 'y24',\n",
       " 'z24',\n",
       " 'v24',\n",
       " 'x25',\n",
       " 'y25',\n",
       " 'z25',\n",
       " 'v25',\n",
       " 'x26',\n",
       " 'y26',\n",
       " 'z26',\n",
       " 'v26',\n",
       " 'x27',\n",
       " 'y27',\n",
       " 'z27',\n",
       " 'v27',\n",
       " 'x28',\n",
       " 'y28',\n",
       " 'z28',\n",
       " 'v28',\n",
       " 'x29',\n",
       " 'y29',\n",
       " 'z29',\n",
       " 'v29',\n",
       " 'x30',\n",
       " 'y30',\n",
       " 'z30',\n",
       " 'v30',\n",
       " 'x31',\n",
       " 'y31',\n",
       " 'z31',\n",
       " 'v31',\n",
       " 'x32',\n",
       " 'y32',\n",
       " 'z32',\n",
       " 'v32',\n",
       " 'x33',\n",
       " 'y33',\n",
       " 'z33',\n",
       " 'v33',\n",
       " 'x34',\n",
       " 'y34',\n",
       " 'z34',\n",
       " 'v34',\n",
       " 'x35',\n",
       " 'y35',\n",
       " 'z35',\n",
       " 'v35',\n",
       " 'x36',\n",
       " 'y36',\n",
       " 'z36',\n",
       " 'v36',\n",
       " 'x37',\n",
       " 'y37',\n",
       " 'z37',\n",
       " 'v37',\n",
       " 'x38',\n",
       " 'y38',\n",
       " 'z38',\n",
       " 'v38',\n",
       " 'x39',\n",
       " 'y39',\n",
       " 'z39',\n",
       " 'v39',\n",
       " 'x40',\n",
       " 'y40',\n",
       " 'z40',\n",
       " 'v40',\n",
       " 'x41',\n",
       " 'y41',\n",
       " 'z41',\n",
       " 'v41',\n",
       " 'x42',\n",
       " 'y42',\n",
       " 'z42',\n",
       " 'v42',\n",
       " 'x43',\n",
       " 'y43',\n",
       " 'z43',\n",
       " 'v43',\n",
       " 'x44',\n",
       " 'y44',\n",
       " 'z44',\n",
       " 'v44',\n",
       " 'x45',\n",
       " 'y45',\n",
       " 'z45',\n",
       " 'v45',\n",
       " 'x46',\n",
       " 'y46',\n",
       " 'z46',\n",
       " 'v46',\n",
       " 'x47',\n",
       " 'y47',\n",
       " 'z47',\n",
       " 'v47',\n",
       " 'x48',\n",
       " 'y48',\n",
       " 'z48',\n",
       " 'v48',\n",
       " 'x49',\n",
       " 'y49',\n",
       " 'z49',\n",
       " 'v49',\n",
       " 'x50',\n",
       " 'y50',\n",
       " 'z50',\n",
       " 'v50',\n",
       " 'x51',\n",
       " 'y51',\n",
       " 'z51',\n",
       " 'v51',\n",
       " 'x52',\n",
       " 'y52',\n",
       " 'z52',\n",
       " 'v52',\n",
       " 'x53',\n",
       " 'y53',\n",
       " 'z53',\n",
       " 'v53',\n",
       " 'x54',\n",
       " 'y54',\n",
       " 'z54',\n",
       " 'v54',\n",
       " 'x55',\n",
       " 'y55',\n",
       " 'z55',\n",
       " 'v55',\n",
       " 'x56',\n",
       " 'y56',\n",
       " 'z56',\n",
       " 'v56',\n",
       " 'x57',\n",
       " 'y57',\n",
       " 'z57',\n",
       " 'v57',\n",
       " 'x58',\n",
       " 'y58',\n",
       " 'z58',\n",
       " 'v58',\n",
       " 'x59',\n",
       " 'y59',\n",
       " 'z59',\n",
       " 'v59',\n",
       " 'x60',\n",
       " 'y60',\n",
       " 'z60',\n",
       " 'v60',\n",
       " 'x61',\n",
       " 'y61',\n",
       " 'z61',\n",
       " 'v61',\n",
       " 'x62',\n",
       " 'y62',\n",
       " 'z62',\n",
       " 'v62',\n",
       " 'x63',\n",
       " 'y63',\n",
       " 'z63',\n",
       " 'v63',\n",
       " 'x64',\n",
       " 'y64',\n",
       " 'z64',\n",
       " 'v64',\n",
       " 'x65',\n",
       " 'y65',\n",
       " 'z65',\n",
       " 'v65',\n",
       " 'x66',\n",
       " 'y66',\n",
       " 'z66',\n",
       " 'v66',\n",
       " 'x67',\n",
       " 'y67',\n",
       " 'z67',\n",
       " 'v67',\n",
       " 'x68',\n",
       " 'y68',\n",
       " 'z68',\n",
       " 'v68',\n",
       " 'x69',\n",
       " 'y69',\n",
       " 'z69',\n",
       " 'v69',\n",
       " 'x70',\n",
       " 'y70',\n",
       " 'z70',\n",
       " 'v70',\n",
       " 'x71',\n",
       " 'y71',\n",
       " 'z71',\n",
       " 'v71',\n",
       " 'x72',\n",
       " 'y72',\n",
       " 'z72',\n",
       " 'v72',\n",
       " 'x73',\n",
       " 'y73',\n",
       " 'z73',\n",
       " 'v73',\n",
       " 'x74',\n",
       " 'y74',\n",
       " 'z74',\n",
       " 'v74',\n",
       " 'x75',\n",
       " 'y75',\n",
       " 'z75',\n",
       " 'v75',\n",
       " 'x76',\n",
       " 'y76',\n",
       " 'z76',\n",
       " 'v76',\n",
       " 'x77',\n",
       " 'y77',\n",
       " 'z77',\n",
       " 'v77',\n",
       " 'x78',\n",
       " 'y78',\n",
       " 'z78',\n",
       " 'v78',\n",
       " 'x79',\n",
       " 'y79',\n",
       " 'z79',\n",
       " 'v79',\n",
       " 'x80',\n",
       " 'y80',\n",
       " 'z80',\n",
       " 'v80',\n",
       " 'x81',\n",
       " 'y81',\n",
       " 'z81',\n",
       " 'v81',\n",
       " 'x82',\n",
       " 'y82',\n",
       " 'z82',\n",
       " 'v82',\n",
       " 'x83',\n",
       " 'y83',\n",
       " 'z83',\n",
       " 'v83',\n",
       " 'x84',\n",
       " 'y84',\n",
       " 'z84',\n",
       " 'v84',\n",
       " 'x85',\n",
       " 'y85',\n",
       " 'z85',\n",
       " 'v85',\n",
       " 'x86',\n",
       " 'y86',\n",
       " 'z86',\n",
       " 'v86',\n",
       " 'x87',\n",
       " 'y87',\n",
       " 'z87',\n",
       " 'v87',\n",
       " 'x88',\n",
       " 'y88',\n",
       " 'z88',\n",
       " 'v88',\n",
       " 'x89',\n",
       " 'y89',\n",
       " 'z89',\n",
       " 'v89',\n",
       " 'x90',\n",
       " 'y90',\n",
       " 'z90',\n",
       " 'v90',\n",
       " 'x91',\n",
       " 'y91',\n",
       " 'z91',\n",
       " 'v91',\n",
       " 'x92',\n",
       " 'y92',\n",
       " 'z92',\n",
       " 'v92',\n",
       " 'x93',\n",
       " 'y93',\n",
       " 'z93',\n",
       " 'v93',\n",
       " 'x94',\n",
       " 'y94',\n",
       " 'z94',\n",
       " 'v94',\n",
       " 'x95',\n",
       " 'y95',\n",
       " 'z95',\n",
       " 'v95',\n",
       " 'x96',\n",
       " 'y96',\n",
       " 'z96',\n",
       " 'v96',\n",
       " 'x97',\n",
       " 'y97',\n",
       " 'z97',\n",
       " 'v97',\n",
       " 'x98',\n",
       " 'y98',\n",
       " 'z98',\n",
       " 'v98',\n",
       " 'x99',\n",
       " 'y99',\n",
       " 'z99',\n",
       " 'v99',\n",
       " 'x100',\n",
       " 'y100',\n",
       " 'z100',\n",
       " 'v100',\n",
       " 'x101',\n",
       " 'y101',\n",
       " 'z101',\n",
       " 'v101',\n",
       " 'x102',\n",
       " 'y102',\n",
       " 'z102',\n",
       " 'v102',\n",
       " 'x103',\n",
       " 'y103',\n",
       " 'z103',\n",
       " 'v103',\n",
       " 'x104',\n",
       " 'y104',\n",
       " 'z104',\n",
       " 'v104',\n",
       " 'x105',\n",
       " 'y105',\n",
       " 'z105',\n",
       " 'v105',\n",
       " 'x106',\n",
       " 'y106',\n",
       " 'z106',\n",
       " 'v106',\n",
       " 'x107',\n",
       " 'y107',\n",
       " 'z107',\n",
       " 'v107',\n",
       " 'x108',\n",
       " 'y108',\n",
       " 'z108',\n",
       " 'v108',\n",
       " 'x109',\n",
       " 'y109',\n",
       " 'z109',\n",
       " 'v109',\n",
       " 'x110',\n",
       " 'y110',\n",
       " 'z110',\n",
       " 'v110',\n",
       " 'x111',\n",
       " 'y111',\n",
       " 'z111',\n",
       " 'v111',\n",
       " 'x112',\n",
       " 'y112',\n",
       " 'z112',\n",
       " 'v112',\n",
       " 'x113',\n",
       " 'y113',\n",
       " 'z113',\n",
       " 'v113',\n",
       " 'x114',\n",
       " 'y114',\n",
       " 'z114',\n",
       " 'v114',\n",
       " 'x115',\n",
       " 'y115',\n",
       " 'z115',\n",
       " 'v115',\n",
       " 'x116',\n",
       " 'y116',\n",
       " 'z116',\n",
       " 'v116',\n",
       " 'x117',\n",
       " 'y117',\n",
       " 'z117',\n",
       " 'v117',\n",
       " 'x118',\n",
       " 'y118',\n",
       " 'z118',\n",
       " 'v118',\n",
       " 'x119',\n",
       " 'y119',\n",
       " 'z119',\n",
       " 'v119',\n",
       " 'x120',\n",
       " 'y120',\n",
       " 'z120',\n",
       " 'v120',\n",
       " 'x121',\n",
       " 'y121',\n",
       " 'z121',\n",
       " 'v121',\n",
       " 'x122',\n",
       " 'y122',\n",
       " 'z122',\n",
       " 'v122',\n",
       " 'x123',\n",
       " 'y123',\n",
       " 'z123',\n",
       " 'v123',\n",
       " 'x124',\n",
       " 'y124',\n",
       " 'z124',\n",
       " 'v124',\n",
       " 'x125',\n",
       " 'y125',\n",
       " 'z125',\n",
       " 'v125',\n",
       " 'x126',\n",
       " 'y126',\n",
       " 'z126',\n",
       " 'v126',\n",
       " 'x127',\n",
       " 'y127',\n",
       " 'z127',\n",
       " 'v127',\n",
       " 'x128',\n",
       " 'y128',\n",
       " 'z128',\n",
       " 'v128',\n",
       " 'x129',\n",
       " 'y129',\n",
       " 'z129',\n",
       " 'v129',\n",
       " 'x130',\n",
       " 'y130',\n",
       " 'z130',\n",
       " 'v130',\n",
       " 'x131',\n",
       " 'y131',\n",
       " 'z131',\n",
       " 'v131',\n",
       " 'x132',\n",
       " 'y132',\n",
       " 'z132',\n",
       " 'v132',\n",
       " 'x133',\n",
       " 'y133',\n",
       " 'z133',\n",
       " 'v133',\n",
       " 'x134',\n",
       " 'y134',\n",
       " 'z134',\n",
       " 'v134',\n",
       " 'x135',\n",
       " 'y135',\n",
       " 'z135',\n",
       " 'v135',\n",
       " 'x136',\n",
       " 'y136',\n",
       " 'z136',\n",
       " 'v136',\n",
       " 'x137',\n",
       " 'y137',\n",
       " 'z137',\n",
       " 'v137',\n",
       " 'x138',\n",
       " 'y138',\n",
       " 'z138',\n",
       " 'v138',\n",
       " 'x139',\n",
       " 'y139',\n",
       " 'z139',\n",
       " 'v139',\n",
       " 'x140',\n",
       " 'y140',\n",
       " 'z140',\n",
       " 'v140',\n",
       " 'x141',\n",
       " 'y141',\n",
       " 'z141',\n",
       " 'v141',\n",
       " 'x142',\n",
       " 'y142',\n",
       " 'z142',\n",
       " 'v142',\n",
       " 'x143',\n",
       " 'y143',\n",
       " 'z143',\n",
       " 'v143',\n",
       " 'x144',\n",
       " 'y144',\n",
       " 'z144',\n",
       " 'v144',\n",
       " 'x145',\n",
       " 'y145',\n",
       " 'z145',\n",
       " 'v145',\n",
       " 'x146',\n",
       " 'y146',\n",
       " 'z146',\n",
       " 'v146',\n",
       " 'x147',\n",
       " 'y147',\n",
       " 'z147',\n",
       " 'v147',\n",
       " 'x148',\n",
       " 'y148',\n",
       " 'z148',\n",
       " 'v148',\n",
       " 'x149',\n",
       " 'y149',\n",
       " 'z149',\n",
       " 'v149',\n",
       " 'x150',\n",
       " 'y150',\n",
       " 'z150',\n",
       " 'v150',\n",
       " 'x151',\n",
       " 'y151',\n",
       " 'z151',\n",
       " 'v151',\n",
       " 'x152',\n",
       " 'y152',\n",
       " 'z152',\n",
       " 'v152',\n",
       " 'x153',\n",
       " 'y153',\n",
       " 'z153',\n",
       " 'v153',\n",
       " 'x154',\n",
       " 'y154',\n",
       " 'z154',\n",
       " 'v154',\n",
       " 'x155',\n",
       " 'y155',\n",
       " 'z155',\n",
       " 'v155',\n",
       " 'x156',\n",
       " 'y156',\n",
       " 'z156',\n",
       " 'v156',\n",
       " 'x157',\n",
       " 'y157',\n",
       " 'z157',\n",
       " 'v157',\n",
       " 'x158',\n",
       " 'y158',\n",
       " 'z158',\n",
       " 'v158',\n",
       " 'x159',\n",
       " 'y159',\n",
       " 'z159',\n",
       " 'v159',\n",
       " 'x160',\n",
       " 'y160',\n",
       " 'z160',\n",
       " 'v160',\n",
       " 'x161',\n",
       " 'y161',\n",
       " 'z161',\n",
       " 'v161',\n",
       " 'x162',\n",
       " 'y162',\n",
       " 'z162',\n",
       " 'v162',\n",
       " 'x163',\n",
       " 'y163',\n",
       " 'z163',\n",
       " 'v163',\n",
       " 'x164',\n",
       " 'y164',\n",
       " 'z164',\n",
       " 'v164',\n",
       " 'x165',\n",
       " 'y165',\n",
       " 'z165',\n",
       " 'v165',\n",
       " 'x166',\n",
       " 'y166',\n",
       " 'z166',\n",
       " 'v166',\n",
       " 'x167',\n",
       " 'y167',\n",
       " 'z167',\n",
       " 'v167',\n",
       " 'x168',\n",
       " 'y168',\n",
       " 'z168',\n",
       " 'v168',\n",
       " 'x169',\n",
       " 'y169',\n",
       " 'z169',\n",
       " 'v169',\n",
       " 'x170',\n",
       " 'y170',\n",
       " 'z170',\n",
       " 'v170',\n",
       " 'x171',\n",
       " 'y171',\n",
       " 'z171',\n",
       " 'v171',\n",
       " 'x172',\n",
       " 'y172',\n",
       " 'z172',\n",
       " 'v172',\n",
       " 'x173',\n",
       " 'y173',\n",
       " 'z173',\n",
       " 'v173',\n",
       " 'x174',\n",
       " 'y174',\n",
       " 'z174',\n",
       " 'v174',\n",
       " 'x175',\n",
       " 'y175',\n",
       " 'z175',\n",
       " 'v175',\n",
       " 'x176',\n",
       " 'y176',\n",
       " 'z176',\n",
       " 'v176',\n",
       " 'x177',\n",
       " 'y177',\n",
       " 'z177',\n",
       " 'v177',\n",
       " 'x178',\n",
       " 'y178',\n",
       " 'z178',\n",
       " 'v178',\n",
       " 'x179',\n",
       " 'y179',\n",
       " 'z179',\n",
       " 'v179',\n",
       " 'x180',\n",
       " 'y180',\n",
       " 'z180',\n",
       " 'v180',\n",
       " 'x181',\n",
       " 'y181',\n",
       " 'z181',\n",
       " 'v181',\n",
       " 'x182',\n",
       " 'y182',\n",
       " 'z182',\n",
       " 'v182',\n",
       " 'x183',\n",
       " 'y183',\n",
       " 'z183',\n",
       " 'v183',\n",
       " 'x184',\n",
       " 'y184',\n",
       " 'z184',\n",
       " 'v184',\n",
       " 'x185',\n",
       " 'y185',\n",
       " 'z185',\n",
       " 'v185',\n",
       " 'x186',\n",
       " 'y186',\n",
       " 'z186',\n",
       " 'v186',\n",
       " 'x187',\n",
       " 'y187',\n",
       " 'z187',\n",
       " 'v187',\n",
       " 'x188',\n",
       " 'y188',\n",
       " 'z188',\n",
       " 'v188',\n",
       " 'x189',\n",
       " 'y189',\n",
       " 'z189',\n",
       " 'v189',\n",
       " 'x190',\n",
       " 'y190',\n",
       " 'z190',\n",
       " 'v190',\n",
       " 'x191',\n",
       " 'y191',\n",
       " 'z191',\n",
       " 'v191',\n",
       " 'x192',\n",
       " 'y192',\n",
       " 'z192',\n",
       " 'v192',\n",
       " 'x193',\n",
       " 'y193',\n",
       " 'z193',\n",
       " 'v193',\n",
       " 'x194',\n",
       " 'y194',\n",
       " 'z194',\n",
       " 'v194',\n",
       " 'x195',\n",
       " 'y195',\n",
       " 'z195',\n",
       " 'v195',\n",
       " 'x196',\n",
       " 'y196',\n",
       " 'z196',\n",
       " 'v196',\n",
       " 'x197',\n",
       " 'y197',\n",
       " 'z197',\n",
       " 'v197',\n",
       " 'x198',\n",
       " 'y198',\n",
       " 'z198',\n",
       " 'v198',\n",
       " 'x199',\n",
       " 'y199',\n",
       " 'z199',\n",
       " 'v199',\n",
       " 'x200',\n",
       " 'y200',\n",
       " 'z200',\n",
       " 'v200',\n",
       " 'x201',\n",
       " 'y201',\n",
       " 'z201',\n",
       " 'v201',\n",
       " 'x202',\n",
       " 'y202',\n",
       " 'z202',\n",
       " 'v202',\n",
       " 'x203',\n",
       " 'y203',\n",
       " 'z203',\n",
       " 'v203',\n",
       " 'x204',\n",
       " 'y204',\n",
       " 'z204',\n",
       " 'v204',\n",
       " 'x205',\n",
       " 'y205',\n",
       " 'z205',\n",
       " 'v205',\n",
       " 'x206',\n",
       " 'y206',\n",
       " 'z206',\n",
       " 'v206',\n",
       " 'x207',\n",
       " 'y207',\n",
       " 'z207',\n",
       " 'v207',\n",
       " 'x208',\n",
       " 'y208',\n",
       " 'z208',\n",
       " 'v208',\n",
       " 'x209',\n",
       " 'y209',\n",
       " 'z209',\n",
       " 'v209',\n",
       " 'x210',\n",
       " 'y210',\n",
       " 'z210',\n",
       " 'v210',\n",
       " 'x211',\n",
       " 'y211',\n",
       " 'z211',\n",
       " 'v211',\n",
       " 'x212',\n",
       " 'y212',\n",
       " 'z212',\n",
       " 'v212',\n",
       " 'x213',\n",
       " 'y213',\n",
       " 'z213',\n",
       " 'v213',\n",
       " 'x214',\n",
       " 'y214',\n",
       " 'z214',\n",
       " 'v214',\n",
       " 'x215',\n",
       " 'y215',\n",
       " 'z215',\n",
       " 'v215',\n",
       " 'x216',\n",
       " 'y216',\n",
       " 'z216',\n",
       " 'v216',\n",
       " 'x217',\n",
       " 'y217',\n",
       " 'z217',\n",
       " 'v217',\n",
       " 'x218',\n",
       " 'y218',\n",
       " 'z218',\n",
       " 'v218',\n",
       " 'x219',\n",
       " 'y219',\n",
       " 'z219',\n",
       " 'v219',\n",
       " 'x220',\n",
       " 'y220',\n",
       " 'z220',\n",
       " 'v220',\n",
       " 'x221',\n",
       " 'y221',\n",
       " 'z221',\n",
       " 'v221',\n",
       " 'x222',\n",
       " 'y222',\n",
       " 'z222',\n",
       " 'v222',\n",
       " 'x223',\n",
       " 'y223',\n",
       " 'z223',\n",
       " 'v223',\n",
       " 'x224',\n",
       " 'y224',\n",
       " 'z224',\n",
       " 'v224',\n",
       " 'x225',\n",
       " 'y225',\n",
       " 'z225',\n",
       " 'v225',\n",
       " 'x226',\n",
       " 'y226',\n",
       " 'z226',\n",
       " 'v226',\n",
       " 'x227',\n",
       " 'y227',\n",
       " 'z227',\n",
       " 'v227',\n",
       " 'x228',\n",
       " 'y228',\n",
       " 'z228',\n",
       " 'v228',\n",
       " 'x229',\n",
       " 'y229',\n",
       " 'z229',\n",
       " 'v229',\n",
       " 'x230',\n",
       " 'y230',\n",
       " 'z230',\n",
       " 'v230',\n",
       " 'x231',\n",
       " 'y231',\n",
       " 'z231',\n",
       " 'v231',\n",
       " 'x232',\n",
       " 'y232',\n",
       " 'z232',\n",
       " 'v232',\n",
       " 'x233',\n",
       " 'y233',\n",
       " 'z233',\n",
       " 'v233',\n",
       " 'x234',\n",
       " 'y234',\n",
       " 'z234',\n",
       " 'v234',\n",
       " 'x235',\n",
       " 'y235',\n",
       " 'z235',\n",
       " 'v235',\n",
       " 'x236',\n",
       " 'y236',\n",
       " 'z236',\n",
       " 'v236',\n",
       " 'x237',\n",
       " 'y237',\n",
       " 'z237',\n",
       " 'v237',\n",
       " 'x238',\n",
       " 'y238',\n",
       " 'z238',\n",
       " 'v238',\n",
       " 'x239',\n",
       " 'y239',\n",
       " 'z239',\n",
       " 'v239',\n",
       " 'x240',\n",
       " 'y240',\n",
       " 'z240',\n",
       " 'v240',\n",
       " 'x241',\n",
       " 'y241',\n",
       " 'z241',\n",
       " 'v241',\n",
       " 'x242',\n",
       " 'y242',\n",
       " 'z242',\n",
       " 'v242',\n",
       " 'x243',\n",
       " 'y243',\n",
       " 'z243',\n",
       " 'v243',\n",
       " 'x244',\n",
       " 'y244',\n",
       " 'z244',\n",
       " 'v244',\n",
       " 'x245',\n",
       " 'y245',\n",
       " 'z245',\n",
       " 'v245',\n",
       " 'x246',\n",
       " 'y246',\n",
       " 'z246',\n",
       " 'v246',\n",
       " 'x247',\n",
       " 'y247',\n",
       " 'z247',\n",
       " 'v247',\n",
       " 'x248',\n",
       " 'y248',\n",
       " 'z248',\n",
       " 'v248',\n",
       " 'x249',\n",
       " 'y249',\n",
       " 'z249',\n",
       " 'v249',\n",
       " 'x250',\n",
       " 'y250',\n",
       " 'z250',\n",
       " ...]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "landmarks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('coords.csv', mode='w', newline='') as f:\n",
    "    csv_writer = csv.writer(f, delimiter=',', quotechar='\"', quoting=csv.QUOTE_MINIMAL)\n",
    "    csv_writer.writerow(landmarks)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "class_name = \"Confusion\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "cap = cv2.VideoCapture(0)\n",
    "# Initiate holistic model\n",
    "with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:\n",
    "    \n",
    "    while cap.isOpened():\n",
    "        ret, frame = cap.read()\n",
    "        \n",
    "        # Recolor Feed\n",
    "        image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n",
    "        image.flags.writeable = False        \n",
    "        \n",
    "        # Make Detections\n",
    "        results = holistic.process(image)\n",
    "        # print(results.face_landmarks)\n",
    "        \n",
    "        # face_landmarks, pose_landmarks, left_hand_landmarks, right_hand_landmarks\n",
    "        \n",
    "        # Recolor image back to BGR for rendering\n",
    "        image.flags.writeable = True   \n",
    "        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\n",
    "        \n",
    "        # 1. Draw face landmarks\n",
    "        mp_drawing.draw_landmarks(image, results.face_landmarks, mp_holistic.FACE_CONNECTIONS, \n",
    "                                 mp_drawing.DrawingSpec(color=(80,110,10), thickness=1, circle_radius=1),\n",
    "                                 mp_drawing.DrawingSpec(color=(80,256,121), thickness=1, circle_radius=1)\n",
    "                                 )\n",
    "        \n",
    "        # 2. Right hand\n",
    "        mp_drawing.draw_landmarks(image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS, \n",
    "                                 mp_drawing.DrawingSpec(color=(80,22,10), thickness=2, circle_radius=4),\n",
    "                                 mp_drawing.DrawingSpec(color=(80,44,121), thickness=2, circle_radius=2)\n",
    "                                 )\n",
    "\n",
    "        # 3. Left Hand\n",
    "        mp_drawing.draw_landmarks(image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS, \n",
    "                                 mp_drawing.DrawingSpec(color=(121,22,76), thickness=2, circle_radius=4),\n",
    "                                 mp_drawing.DrawingSpec(color=(121,44,250), thickness=2, circle_radius=2)\n",
    "                                 )\n",
    "\n",
    "        # 4. Pose Detections\n",
    "        mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_holistic.POSE_CONNECTIONS, \n",
    "                                 mp_drawing.DrawingSpec(color=(245,117,66), thickness=2, circle_radius=4),\n",
    "                                 mp_drawing.DrawingSpec(color=(245,66,230), thickness=2, circle_radius=2)\n",
    "                                 )\n",
    "        # Export coordinates\n",
    "        try:\n",
    "            # Extract Pose landmarks\n",
    "            pose = results.pose_landmarks.landmark\n",
    "            pose_row = list(np.array([[landmark.x, landmark.y, landmark.z, landmark.visibility] for landmark in pose]).flatten())\n",
    "            \n",
    "            # Extract Face landmarks\n",
    "            face = results.face_landmarks.landmark\n",
    "            face_row = list(np.array([[landmark.x, landmark.y, landmark.z, landmark.visibility] for landmark in face]).flatten())\n",
    "            \n",
    "            # Concate rows\n",
    "            row = pose_row+face_row\n",
    "            \n",
    "            # Append class name \n",
    "            row.insert(0, class_name)\n",
    "            \n",
    "            # Export to CSV\n",
    "            with open('coords.csv', mode='a', newline='') as f:\n",
    "                csv_writer = csv.writer(f, delimiter=',', quotechar='\"', quoting=csv.QUOTE_MINIMAL)\n",
    "                csv_writer.writerow(row) \n",
    "            \n",
    "        except:\n",
    "            pass\n",
    "                        \n",
    "        cv2.imshow('Raw Webcam Feed', image)\n",
    "\n",
    "        if cv2.waitKey(10) & 0xFF == ord('q'):\n",
    "            break\n",
    "\n",
    "cap.release()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. Train Custom Model Using Scikit Learn"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.1 Read in Collected Data and Process"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('coords.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>class</th>\n",
       "      <th>x1</th>\n",
       "      <th>y1</th>\n",
       "      <th>z1</th>\n",
       "      <th>v1</th>\n",
       "      <th>x2</th>\n",
       "      <th>y2</th>\n",
       "      <th>z2</th>\n",
       "      <th>v2</th>\n",
       "      <th>x3</th>\n",
       "      <th>...</th>\n",
       "      <th>z499</th>\n",
       "      <th>v499</th>\n",
       "      <th>x500</th>\n",
       "      <th>y500</th>\n",
       "      <th>z500</th>\n",
       "      <th>v500</th>\n",
       "      <th>x501</th>\n",
       "      <th>y501</th>\n",
       "      <th>z501</th>\n",
       "      <th>v501</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>Happy</td>\n",
       "      <td>0.518314</td>\n",
       "      <td>0.735723</td>\n",
       "      <td>-0.920691</td>\n",
       "      <td>0.999949</td>\n",
       "      <td>0.542168</td>\n",
       "      <td>0.673646</td>\n",
       "      <td>-0.857515</td>\n",
       "      <td>0.999869</td>\n",
       "      <td>0.560686</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.015950</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.593288</td>\n",
       "      <td>0.657443</td>\n",
       "      <td>0.004889</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.597319</td>\n",
       "      <td>0.653256</td>\n",
       "      <td>0.005069</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>Happy</td>\n",
       "      <td>0.518415</td>\n",
       "      <td>0.735719</td>\n",
       "      <td>-0.977890</td>\n",
       "      <td>0.999934</td>\n",
       "      <td>0.542399</td>\n",
       "      <td>0.673037</td>\n",
       "      <td>-0.909647</td>\n",
       "      <td>0.999836</td>\n",
       "      <td>0.561067</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.015280</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.590493</td>\n",
       "      <td>0.660329</td>\n",
       "      <td>0.005591</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.594543</td>\n",
       "      <td>0.656407</td>\n",
       "      <td>0.005861</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>Happy</td>\n",
       "      <td>0.518249</td>\n",
       "      <td>0.736459</td>\n",
       "      <td>-0.966859</td>\n",
       "      <td>0.999925</td>\n",
       "      <td>0.542268</td>\n",
       "      <td>0.673103</td>\n",
       "      <td>-0.900091</td>\n",
       "      <td>0.999817</td>\n",
       "      <td>0.560940</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.016151</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.589704</td>\n",
       "      <td>0.663581</td>\n",
       "      <td>0.004517</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.593641</td>\n",
       "      <td>0.660065</td>\n",
       "      <td>0.004664</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>Happy</td>\n",
       "      <td>0.517726</td>\n",
       "      <td>0.737293</td>\n",
       "      <td>-0.992221</td>\n",
       "      <td>0.999923</td>\n",
       "      <td>0.542056</td>\n",
       "      <td>0.673201</td>\n",
       "      <td>-0.925868</td>\n",
       "      <td>0.999814</td>\n",
       "      <td>0.560825</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.016158</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.588877</td>\n",
       "      <td>0.659637</td>\n",
       "      <td>0.005202</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.592951</td>\n",
       "      <td>0.655981</td>\n",
       "      <td>0.005390</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>Happy</td>\n",
       "      <td>0.517376</td>\n",
       "      <td>0.737757</td>\n",
       "      <td>-0.997600</td>\n",
       "      <td>0.999912</td>\n",
       "      <td>0.541688</td>\n",
       "      <td>0.673266</td>\n",
       "      <td>-0.931197</td>\n",
       "      <td>0.999788</td>\n",
       "      <td>0.560491</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.015227</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.587802</td>\n",
       "      <td>0.660864</td>\n",
       "      <td>0.005513</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.591846</td>\n",
       "      <td>0.657080</td>\n",
       "      <td>0.005716</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 2005 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   class        x1        y1        z1        v1        x2        y2  \\\n",
       "0  Happy  0.518314  0.735723 -0.920691  0.999949  0.542168  0.673646   \n",
       "1  Happy  0.518415  0.735719 -0.977890  0.999934  0.542399  0.673037   \n",
       "2  Happy  0.518249  0.736459 -0.966859  0.999925  0.542268  0.673103   \n",
       "3  Happy  0.517726  0.737293 -0.992221  0.999923  0.542056  0.673201   \n",
       "4  Happy  0.517376  0.737757 -0.997600  0.999912  0.541688  0.673266   \n",
       "\n",
       "         z2        v2        x3  ...      z499  v499      x500      y500  \\\n",
       "0 -0.857515  0.999869  0.560686  ... -0.015950   0.0  0.593288  0.657443   \n",
       "1 -0.909647  0.999836  0.561067  ... -0.015280   0.0  0.590493  0.660329   \n",
       "2 -0.900091  0.999817  0.560940  ... -0.016151   0.0  0.589704  0.663581   \n",
       "3 -0.925868  0.999814  0.560825  ... -0.016158   0.0  0.588877  0.659637   \n",
       "4 -0.931197  0.999788  0.560491  ... -0.015227   0.0  0.587802  0.660864   \n",
       "\n",
       "       z500  v500      x501      y501      z501  v501  \n",
       "0  0.004889   0.0  0.597319  0.653256  0.005069   0.0  \n",
       "1  0.005591   0.0  0.594543  0.656407  0.005861   0.0  \n",
       "2  0.004517   0.0  0.593641  0.660065  0.004664   0.0  \n",
       "3  0.005202   0.0  0.592951  0.655981  0.005390   0.0  \n",
       "4  0.005513   0.0  0.591846  0.657080  0.005716   0.0  \n",
       "\n",
       "[5 rows x 2005 columns]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>class</th>\n",
       "      <th>x1</th>\n",
       "      <th>y1</th>\n",
       "      <th>z1</th>\n",
       "      <th>v1</th>\n",
       "      <th>x2</th>\n",
       "      <th>y2</th>\n",
       "      <th>z2</th>\n",
       "      <th>v2</th>\n",
       "      <th>x3</th>\n",
       "      <th>...</th>\n",
       "      <th>z499</th>\n",
       "      <th>v499</th>\n",
       "      <th>x500</th>\n",
       "      <th>y500</th>\n",
       "      <th>z500</th>\n",
       "      <th>v500</th>\n",
       "      <th>x501</th>\n",
       "      <th>y501</th>\n",
       "      <th>z501</th>\n",
       "      <th>v501</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>513</td>\n",
       "      <td>Confusion</td>\n",
       "      <td>0.588986</td>\n",
       "      <td>0.635138</td>\n",
       "      <td>-0.939895</td>\n",
       "      <td>0.999854</td>\n",
       "      <td>0.611223</td>\n",
       "      <td>0.579167</td>\n",
       "      <td>-0.861905</td>\n",
       "      <td>0.999822</td>\n",
       "      <td>0.629943</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.005868</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.662890</td>\n",
       "      <td>0.618888</td>\n",
       "      <td>0.016429</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.668906</td>\n",
       "      <td>0.614323</td>\n",
       "      <td>0.017280</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>514</td>\n",
       "      <td>Confusion</td>\n",
       "      <td>0.594993</td>\n",
       "      <td>0.634883</td>\n",
       "      <td>-0.936987</td>\n",
       "      <td>0.999859</td>\n",
       "      <td>0.616767</td>\n",
       "      <td>0.579267</td>\n",
       "      <td>-0.860667</td>\n",
       "      <td>0.999825</td>\n",
       "      <td>0.635379</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.005126</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.664401</td>\n",
       "      <td>0.617123</td>\n",
       "      <td>0.018555</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.670649</td>\n",
       "      <td>0.612389</td>\n",
       "      <td>0.019469</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>515</td>\n",
       "      <td>Confusion</td>\n",
       "      <td>0.594583</td>\n",
       "      <td>0.634793</td>\n",
       "      <td>-0.932667</td>\n",
       "      <td>0.999866</td>\n",
       "      <td>0.617179</td>\n",
       "      <td>0.579249</td>\n",
       "      <td>-0.859610</td>\n",
       "      <td>0.999833</td>\n",
       "      <td>0.636088</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.005307</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.661936</td>\n",
       "      <td>0.615505</td>\n",
       "      <td>0.017020</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.668259</td>\n",
       "      <td>0.611051</td>\n",
       "      <td>0.017806</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>516</td>\n",
       "      <td>Confusion</td>\n",
       "      <td>0.594073</td>\n",
       "      <td>0.634845</td>\n",
       "      <td>-0.911422</td>\n",
       "      <td>0.999874</td>\n",
       "      <td>0.617087</td>\n",
       "      <td>0.579368</td>\n",
       "      <td>-0.839624</td>\n",
       "      <td>0.999841</td>\n",
       "      <td>0.635955</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.006188</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.660923</td>\n",
       "      <td>0.614082</td>\n",
       "      <td>0.015764</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.667228</td>\n",
       "      <td>0.609368</td>\n",
       "      <td>0.016610</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>517</td>\n",
       "      <td>Confusion</td>\n",
       "      <td>0.593430</td>\n",
       "      <td>0.635009</td>\n",
       "      <td>-0.910981</td>\n",
       "      <td>0.999881</td>\n",
       "      <td>0.616957</td>\n",
       "      <td>0.579679</td>\n",
       "      <td>-0.843262</td>\n",
       "      <td>0.999848</td>\n",
       "      <td>0.635765</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.005315</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.658274</td>\n",
       "      <td>0.613182</td>\n",
       "      <td>0.016129</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.664657</td>\n",
       "      <td>0.608317</td>\n",
       "      <td>0.016966</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 2005 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         class        x1        y1        z1        v1        x2        y2  \\\n",
       "513  Confusion  0.588986  0.635138 -0.939895  0.999854  0.611223  0.579167   \n",
       "514  Confusion  0.594993  0.634883 -0.936987  0.999859  0.616767  0.579267   \n",
       "515  Confusion  0.594583  0.634793 -0.932667  0.999866  0.617179  0.579249   \n",
       "516  Confusion  0.594073  0.634845 -0.911422  0.999874  0.617087  0.579368   \n",
       "517  Confusion  0.593430  0.635009 -0.910981  0.999881  0.616957  0.579679   \n",
       "\n",
       "           z2        v2        x3  ...      z499  v499      x500      y500  \\\n",
       "513 -0.861905  0.999822  0.629943  ... -0.005868   0.0  0.662890  0.618888   \n",
       "514 -0.860667  0.999825  0.635379  ... -0.005126   0.0  0.664401  0.617123   \n",
       "515 -0.859610  0.999833  0.636088  ... -0.005307   0.0  0.661936  0.615505   \n",
       "516 -0.839624  0.999841  0.635955  ... -0.006188   0.0  0.660923  0.614082   \n",
       "517 -0.843262  0.999848  0.635765  ... -0.005315   0.0  0.658274  0.613182   \n",
       "\n",
       "         z500  v500      x501      y501      z501  v501  \n",
       "513  0.016429   0.0  0.668906  0.614323  0.017280   0.0  \n",
       "514  0.018555   0.0  0.670649  0.612389  0.019469   0.0  \n",
       "515  0.017020   0.0  0.668259  0.611051  0.017806   0.0  \n",
       "516  0.015764   0.0  0.667228  0.609368  0.016610   0.0  \n",
       "517  0.016129   0.0  0.664657  0.608317  0.016966   0.0  \n",
       "\n",
       "[5 rows x 2005 columns]"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>class</th>\n",
       "      <th>x1</th>\n",
       "      <th>y1</th>\n",
       "      <th>z1</th>\n",
       "      <th>v1</th>\n",
       "      <th>x2</th>\n",
       "      <th>y2</th>\n",
       "      <th>z2</th>\n",
       "      <th>v2</th>\n",
       "      <th>x3</th>\n",
       "      <th>...</th>\n",
       "      <th>z499</th>\n",
       "      <th>v499</th>\n",
       "      <th>x500</th>\n",
       "      <th>y500</th>\n",
       "      <th>z500</th>\n",
       "      <th>v500</th>\n",
       "      <th>x501</th>\n",
       "      <th>y501</th>\n",
       "      <th>z501</th>\n",
       "      <th>v501</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>260</td>\n",
       "      <td>Victory</td>\n",
       "      <td>0.533544</td>\n",
       "      <td>0.641093</td>\n",
       "      <td>-0.988847</td>\n",
       "      <td>0.999964</td>\n",
       "      <td>0.554349</td>\n",
       "      <td>0.581441</td>\n",
       "      <td>-0.923734</td>\n",
       "      <td>0.999921</td>\n",
       "      <td>0.572255</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.008839</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.590815</td>\n",
       "      <td>0.576881</td>\n",
       "      <td>0.010013</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.594678</td>\n",
       "      <td>0.573483</td>\n",
       "      <td>0.010165</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>261</td>\n",
       "      <td>Victory</td>\n",
       "      <td>0.531331</td>\n",
       "      <td>0.641071</td>\n",
       "      <td>-1.001869</td>\n",
       "      <td>0.999966</td>\n",
       "      <td>0.553496</td>\n",
       "      <td>0.581324</td>\n",
       "      <td>-0.941450</td>\n",
       "      <td>0.999925</td>\n",
       "      <td>0.571518</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.010927</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.586696</td>\n",
       "      <td>0.573635</td>\n",
       "      <td>0.007833</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.590752</td>\n",
       "      <td>0.569735</td>\n",
       "      <td>0.007937</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>262</td>\n",
       "      <td>Victory</td>\n",
       "      <td>0.529470</td>\n",
       "      <td>0.641075</td>\n",
       "      <td>-1.026511</td>\n",
       "      <td>0.999968</td>\n",
       "      <td>0.552432</td>\n",
       "      <td>0.580922</td>\n",
       "      <td>-0.971897</td>\n",
       "      <td>0.999931</td>\n",
       "      <td>0.570740</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.011643</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.587498</td>\n",
       "      <td>0.569356</td>\n",
       "      <td>0.006461</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.591465</td>\n",
       "      <td>0.565474</td>\n",
       "      <td>0.006487</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>263</td>\n",
       "      <td>Victory</td>\n",
       "      <td>0.528755</td>\n",
       "      <td>0.641172</td>\n",
       "      <td>-1.023675</td>\n",
       "      <td>0.999970</td>\n",
       "      <td>0.552067</td>\n",
       "      <td>0.580729</td>\n",
       "      <td>-0.968875</td>\n",
       "      <td>0.999935</td>\n",
       "      <td>0.570549</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.011314</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.589247</td>\n",
       "      <td>0.568398</td>\n",
       "      <td>0.007734</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.593355</td>\n",
       "      <td>0.564562</td>\n",
       "      <td>0.007837</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>264</td>\n",
       "      <td>Victory</td>\n",
       "      <td>0.527552</td>\n",
       "      <td>0.641101</td>\n",
       "      <td>-1.026600</td>\n",
       "      <td>0.999972</td>\n",
       "      <td>0.551127</td>\n",
       "      <td>0.579952</td>\n",
       "      <td>-0.971607</td>\n",
       "      <td>0.999939</td>\n",
       "      <td>0.569783</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.012244</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.589656</td>\n",
       "      <td>0.566831</td>\n",
       "      <td>0.006789</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.593742</td>\n",
       "      <td>0.562972</td>\n",
       "      <td>0.006836</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>401</td>\n",
       "      <td>Victory</td>\n",
       "      <td>0.563860</td>\n",
       "      <td>0.646863</td>\n",
       "      <td>-0.909361</td>\n",
       "      <td>0.999945</td>\n",
       "      <td>0.588460</td>\n",
       "      <td>0.581305</td>\n",
       "      <td>-0.808727</td>\n",
       "      <td>0.999893</td>\n",
       "      <td>0.608051</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.016492</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.609819</td>\n",
       "      <td>0.585942</td>\n",
       "      <td>0.001513</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.613367</td>\n",
       "      <td>0.581925</td>\n",
       "      <td>0.001591</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>402</td>\n",
       "      <td>Victory</td>\n",
       "      <td>0.553518</td>\n",
       "      <td>0.646257</td>\n",
       "      <td>-0.915329</td>\n",
       "      <td>0.999940</td>\n",
       "      <td>0.577025</td>\n",
       "      <td>0.581027</td>\n",
       "      <td>-0.816409</td>\n",
       "      <td>0.999882</td>\n",
       "      <td>0.594985</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.015409</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.607840</td>\n",
       "      <td>0.588869</td>\n",
       "      <td>0.002822</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.611426</td>\n",
       "      <td>0.584958</td>\n",
       "      <td>0.002927</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>403</td>\n",
       "      <td>Victory</td>\n",
       "      <td>0.546836</td>\n",
       "      <td>0.646258</td>\n",
       "      <td>-0.947897</td>\n",
       "      <td>0.999941</td>\n",
       "      <td>0.572246</td>\n",
       "      <td>0.581272</td>\n",
       "      <td>-0.893420</td>\n",
       "      <td>0.999882</td>\n",
       "      <td>0.590308</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.015218</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.603719</td>\n",
       "      <td>0.584568</td>\n",
       "      <td>0.002761</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.607370</td>\n",
       "      <td>0.580642</td>\n",
       "      <td>0.002848</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>404</td>\n",
       "      <td>Victory</td>\n",
       "      <td>0.545863</td>\n",
       "      <td>0.645397</td>\n",
       "      <td>-1.044260</td>\n",
       "      <td>0.999941</td>\n",
       "      <td>0.571006</td>\n",
       "      <td>0.579781</td>\n",
       "      <td>-0.987232</td>\n",
       "      <td>0.999880</td>\n",
       "      <td>0.589387</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.015993</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.603986</td>\n",
       "      <td>0.577291</td>\n",
       "      <td>0.002490</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.607842</td>\n",
       "      <td>0.572501</td>\n",
       "      <td>0.002662</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>405</td>\n",
       "      <td>Victory</td>\n",
       "      <td>0.546216</td>\n",
       "      <td>0.641825</td>\n",
       "      <td>-1.063106</td>\n",
       "      <td>0.999940</td>\n",
       "      <td>0.570889</td>\n",
       "      <td>0.574558</td>\n",
       "      <td>-0.999432</td>\n",
       "      <td>0.999874</td>\n",
       "      <td>0.589394</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.014844</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.600757</td>\n",
       "      <td>0.574284</td>\n",
       "      <td>0.004119</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.604511</td>\n",
       "      <td>0.569942</td>\n",
       "      <td>0.004314</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>146 rows × 2005 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       class        x1        y1        z1        v1        x2        y2  \\\n",
       "260  Victory  0.533544  0.641093 -0.988847  0.999964  0.554349  0.581441   \n",
       "261  Victory  0.531331  0.641071 -1.001869  0.999966  0.553496  0.581324   \n",
       "262  Victory  0.529470  0.641075 -1.026511  0.999968  0.552432  0.580922   \n",
       "263  Victory  0.528755  0.641172 -1.023675  0.999970  0.552067  0.580729   \n",
       "264  Victory  0.527552  0.641101 -1.026600  0.999972  0.551127  0.579952   \n",
       "..       ...       ...       ...       ...       ...       ...       ...   \n",
       "401  Victory  0.563860  0.646863 -0.909361  0.999945  0.588460  0.581305   \n",
       "402  Victory  0.553518  0.646257 -0.915329  0.999940  0.577025  0.581027   \n",
       "403  Victory  0.546836  0.646258 -0.947897  0.999941  0.572246  0.581272   \n",
       "404  Victory  0.545863  0.645397 -1.044260  0.999941  0.571006  0.579781   \n",
       "405  Victory  0.546216  0.641825 -1.063106  0.999940  0.570889  0.574558   \n",
       "\n",
       "           z2        v2        x3  ...      z499  v499      x500      y500  \\\n",
       "260 -0.923734  0.999921  0.572255  ... -0.008839   0.0  0.590815  0.576881   \n",
       "261 -0.941450  0.999925  0.571518  ... -0.010927   0.0  0.586696  0.573635   \n",
       "262 -0.971897  0.999931  0.570740  ... -0.011643   0.0  0.587498  0.569356   \n",
       "263 -0.968875  0.999935  0.570549  ... -0.011314   0.0  0.589247  0.568398   \n",
       "264 -0.971607  0.999939  0.569783  ... -0.012244   0.0  0.589656  0.566831   \n",
       "..        ...       ...       ...  ...       ...   ...       ...       ...   \n",
       "401 -0.808727  0.999893  0.608051  ... -0.016492   0.0  0.609819  0.585942   \n",
       "402 -0.816409  0.999882  0.594985  ... -0.015409   0.0  0.607840  0.588869   \n",
       "403 -0.893420  0.999882  0.590308  ... -0.015218   0.0  0.603719  0.584568   \n",
       "404 -0.987232  0.999880  0.589387  ... -0.015993   0.0  0.603986  0.577291   \n",
       "405 -0.999432  0.999874  0.589394  ... -0.014844   0.0  0.600757  0.574284   \n",
       "\n",
       "         z500  v500      x501      y501      z501  v501  \n",
       "260  0.010013   0.0  0.594678  0.573483  0.010165   0.0  \n",
       "261  0.007833   0.0  0.590752  0.569735  0.007937   0.0  \n",
       "262  0.006461   0.0  0.591465  0.565474  0.006487   0.0  \n",
       "263  0.007734   0.0  0.593355  0.564562  0.007837   0.0  \n",
       "264  0.006789   0.0  0.593742  0.562972  0.006836   0.0  \n",
       "..        ...   ...       ...       ...       ...   ...  \n",
       "401  0.001513   0.0  0.613367  0.581925  0.001591   0.0  \n",
       "402  0.002822   0.0  0.611426  0.584958  0.002927   0.0  \n",
       "403  0.002761   0.0  0.607370  0.580642  0.002848   0.0  \n",
       "404  0.002490   0.0  0.607842  0.572501  0.002662   0.0  \n",
       "405  0.004119   0.0  0.604511  0.569942  0.004314   0.0  \n",
       "\n",
       "[146 rows x 2005 columns]"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df['class']=='Victory']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = df.drop('class', axis=1) # features\n",
    "y = df['class'] # target value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1234)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "373      Victory\n",
       "131        Happy\n",
       "214          Sad\n",
       "297      Victory\n",
       "67         Happy\n",
       "         ...    \n",
       "499    Confusion\n",
       "90         Happy\n",
       "156          Sad\n",
       "296      Victory\n",
       "300      Victory\n",
       "Name: class, Length: 156, dtype: object"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.2 Train Machine Learning Classification Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.pipeline import make_pipeline \n",
    "from sklearn.preprocessing import StandardScaler \n",
    "\n",
    "from sklearn.linear_model import LogisticRegression, RidgeClassifier\n",
    "from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "pipelines = {\n",
    "    'lr':make_pipeline(StandardScaler(), LogisticRegression()),\n",
    "    'rc':make_pipeline(StandardScaler(), RidgeClassifier()),\n",
    "    'rf':make_pipeline(StandardScaler(), RandomForestClassifier()),\n",
    "    'gb':make_pipeline(StandardScaler(), GradientBoostingClassifier()),\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\sklearn\\linear_model\\logistic.py:432: FutureWarning: Default solver will be changed to 'lbfgs' in 0.22. Specify a solver to silence this warning.\n",
      "  FutureWarning)\n",
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\sklearn\\linear_model\\logistic.py:469: FutureWarning: Default multi_class will be changed to 'auto' in 0.22. Specify the multi_class option to silence this warning.\n",
      "  \"this warning.\", FutureWarning)\n",
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\sklearn\\ensemble\\forest.py:245: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22.\n",
      "  \"10 in version 0.20 to 100 in 0.22.\", FutureWarning)\n"
     ]
    }
   ],
   "source": [
    "fit_models = {}\n",
    "for algo, pipeline in pipelines.items():\n",
    "    model = pipeline.fit(X_train, y_train)\n",
    "    fit_models[algo] = model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'lr': Pipeline(memory=None,\n",
       "          steps=[('standardscaler',\n",
       "                  StandardScaler(copy=True, with_mean=True, with_std=True)),\n",
       "                 ('logisticregression',\n",
       "                  LogisticRegression(C=1.0, class_weight=None, dual=False,\n",
       "                                     fit_intercept=True, intercept_scaling=1,\n",
       "                                     l1_ratio=None, max_iter=100,\n",
       "                                     multi_class='warn', n_jobs=None,\n",
       "                                     penalty='l2', random_state=None,\n",
       "                                     solver='warn', tol=0.0001, verbose=0,\n",
       "                                     warm_start=False))],\n",
       "          verbose=False), 'rc': Pipeline(memory=None,\n",
       "          steps=[('standardscaler',\n",
       "                  StandardScaler(copy=True, with_mean=True, with_std=True)),\n",
       "                 ('ridgeclassifier',\n",
       "                  RidgeClassifier(alpha=1.0, class_weight=None, copy_X=True,\n",
       "                                  fit_intercept=True, max_iter=None,\n",
       "                                  normalize=False, random_state=None,\n",
       "                                  solver='auto', tol=0.001))],\n",
       "          verbose=False), 'rf': Pipeline(memory=None,\n",
       "          steps=[('standardscaler',\n",
       "                  StandardScaler(copy=True, with_mean=True, with_std=True)),\n",
       "                 ('randomforestclassifier',\n",
       "                  RandomForestClassifier(bootstrap=True, class_weight=None,\n",
       "                                         criterion='gini', max_depth=None,\n",
       "                                         max_features='auto',\n",
       "                                         max_leaf_nodes=None,\n",
       "                                         min_impurity_decrease=0.0,\n",
       "                                         min_impurity_split=None,\n",
       "                                         min_samples_leaf=1, min_samples_split=2,\n",
       "                                         min_weight_fraction_leaf=0.0,\n",
       "                                         n_estimators=10, n_jobs=None,\n",
       "                                         oob_score=False, random_state=None,\n",
       "                                         verbose=0, warm_start=False))],\n",
       "          verbose=False), 'gb': Pipeline(memory=None,\n",
       "          steps=[('standardscaler',\n",
       "                  StandardScaler(copy=True, with_mean=True, with_std=True)),\n",
       "                 ('gradientboostingclassifier',\n",
       "                  GradientBoostingClassifier(criterion='friedman_mse', init=None,\n",
       "                                             learning_rate=0.1, loss='deviance',\n",
       "                                             max_depth=3, max_features=None,\n",
       "                                             max_leaf_nodes=None,\n",
       "                                             min_impurity_decrease=0.0,\n",
       "                                             min_impurity_split=None,\n",
       "                                             min_samples_leaf=1,\n",
       "                                             min_samples_split=2,\n",
       "                                             min_weight_fraction_leaf=0.0,\n",
       "                                             n_estimators=100,\n",
       "                                             n_iter_no_change=None,\n",
       "                                             presort='auto', random_state=None,\n",
       "                                             subsample=1.0, tol=0.0001,\n",
       "                                             validation_fraction=0.1, verbose=0,\n",
       "                                             warm_start=False))],\n",
       "          verbose=False)}"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fit_models"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['Victory', 'Happy', 'Sad', 'Victory', 'Happy', 'Happy', 'Victory',\n",
       "       'Happy', 'Sad', 'Victory', 'Sad', 'Sad', 'Happy', 'Victory',\n",
       "       'Happy', 'Sad', 'Happy', 'Happy', 'Confusion', 'Confusion',\n",
       "       'Happy', 'Sad', 'Victory', 'Sad', 'Happy', 'Victory', 'Victory',\n",
       "       'Confusion', 'Happy', 'Victory', 'Victory', 'Happy', 'Happy',\n",
       "       'Happy', 'Victory', 'Victory', 'Confusion', 'Victory', 'Victory',\n",
       "       'Confusion', 'Happy', 'Sad', 'Happy', 'Happy', 'Victory', 'Happy',\n",
       "       'Happy', 'Victory', 'Confusion', 'Happy', 'Sad', 'Happy',\n",
       "       'Victory', 'Victory', 'Confusion', 'Confusion', 'Happy',\n",
       "       'Confusion', 'Happy', 'Victory', 'Victory', 'Victory', 'Sad',\n",
       "       'Confusion', 'Happy', 'Confusion', 'Confusion', 'Confusion',\n",
       "       'Happy', 'Sad', 'Victory', 'Victory', 'Confusion', 'Happy',\n",
       "       'Confusion', 'Happy', 'Sad', 'Victory', 'Happy', 'Happy', 'Sad',\n",
       "       'Victory', 'Victory', 'Confusion', 'Sad', 'Sad', 'Happy', 'Sad',\n",
       "       'Victory', 'Victory', 'Sad', 'Sad', 'Happy', 'Happy', 'Happy',\n",
       "       'Victory', 'Confusion', 'Victory', 'Happy', 'Happy', 'Confusion',\n",
       "       'Victory', 'Victory', 'Victory', 'Confusion', 'Sad', 'Victory',\n",
       "       'Confusion', 'Victory', 'Sad', 'Victory', 'Victory', 'Confusion',\n",
       "       'Sad', 'Confusion', 'Victory', 'Happy', 'Victory', 'Confusion',\n",
       "       'Confusion', 'Confusion', 'Victory', 'Confusion', 'Happy',\n",
       "       'Confusion', 'Victory', 'Sad', 'Victory', 'Happy', 'Victory',\n",
       "       'Happy', 'Happy', 'Sad', 'Happy', 'Confusion', 'Happy',\n",
       "       'Confusion', 'Confusion', 'Confusion', 'Happy', 'Victory',\n",
       "       'Victory', 'Happy', 'Confusion', 'Confusion', 'Victory', 'Happy',\n",
       "       'Sad', 'Victory', 'Sad', 'Victory', 'Confusion', 'Happy', 'Sad',\n",
       "       'Victory', 'Victory'], dtype='<U9')"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fit_models['rc'].predict(X_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.3 Evaluate and Serialize Model "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import accuracy_score # Accuracy metrics \n",
    "import pickle "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lr 1.0\n",
      "rc 1.0\n",
      "rf 0.9871794871794872\n",
      "gb 0.9871794871794872\n"
     ]
    }
   ],
   "source": [
    "for algo, model in fit_models.items():\n",
    "    yhat = model.predict(X_test)\n",
    "    print(algo, accuracy_score(y_test, yhat))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['Victory', 'Happy', 'Sad', 'Victory', 'Happy', 'Happy', 'Victory',\n",
       "       'Sad', 'Sad', 'Victory', 'Sad', 'Sad', 'Happy', 'Victory', 'Happy',\n",
       "       'Sad', 'Happy', 'Happy', 'Confusion', 'Confusion', 'Happy', 'Sad',\n",
       "       'Victory', 'Sad', 'Happy', 'Victory', 'Victory', 'Confusion',\n",
       "       'Happy', 'Victory', 'Victory', 'Happy', 'Happy', 'Happy',\n",
       "       'Victory', 'Victory', 'Confusion', 'Victory', 'Victory',\n",
       "       'Confusion', 'Happy', 'Sad', 'Happy', 'Happy', 'Victory', 'Happy',\n",
       "       'Happy', 'Victory', 'Confusion', 'Happy', 'Sad', 'Happy',\n",
       "       'Victory', 'Victory', 'Confusion', 'Confusion', 'Happy',\n",
       "       'Confusion', 'Happy', 'Victory', 'Victory', 'Victory', 'Sad',\n",
       "       'Confusion', 'Happy', 'Confusion', 'Confusion', 'Confusion',\n",
       "       'Happy', 'Sad', 'Victory', 'Victory', 'Confusion', 'Happy',\n",
       "       'Confusion', 'Happy', 'Sad', 'Victory', 'Happy', 'Happy', 'Sad',\n",
       "       'Victory', 'Victory', 'Confusion', 'Sad', 'Sad', 'Happy', 'Sad',\n",
       "       'Victory', 'Victory', 'Sad', 'Sad', 'Happy', 'Happy', 'Happy',\n",
       "       'Victory', 'Confusion', 'Victory', 'Happy', 'Happy', 'Confusion',\n",
       "       'Sad', 'Victory', 'Victory', 'Confusion', 'Sad', 'Victory',\n",
       "       'Confusion', 'Victory', 'Sad', 'Victory', 'Victory', 'Confusion',\n",
       "       'Sad', 'Confusion', 'Victory', 'Happy', 'Victory', 'Confusion',\n",
       "       'Confusion', 'Confusion', 'Victory', 'Confusion', 'Happy',\n",
       "       'Confusion', 'Victory', 'Sad', 'Victory', 'Happy', 'Victory',\n",
       "       'Happy', 'Happy', 'Sad', 'Happy', 'Confusion', 'Happy',\n",
       "       'Confusion', 'Confusion', 'Confusion', 'Happy', 'Victory',\n",
       "       'Victory', 'Happy', 'Confusion', 'Confusion', 'Victory', 'Happy',\n",
       "       'Sad', 'Victory', 'Sad', 'Victory', 'Confusion', 'Happy', 'Sad',\n",
       "       'Victory', 'Victory'], dtype=object)"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fit_models['rf'].predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "373      Victory\n",
       "131        Happy\n",
       "214          Sad\n",
       "297      Victory\n",
       "67         Happy\n",
       "         ...    \n",
       "499    Confusion\n",
       "90         Happy\n",
       "156          Sad\n",
       "296      Victory\n",
       "300      Victory\n",
       "Name: class, Length: 156, dtype: object"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('body_language.pkl', 'wb') as f:\n",
    "    pickle.dump(fit_models['rf'], f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. Make Detections with Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('body_language.pkl', 'rb') as f:\n",
    "    model = pickle.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Pipeline(memory=None,\n",
       "         steps=[('standardscaler',\n",
       "                 StandardScaler(copy=True, with_mean=True, with_std=True)),\n",
       "                ('randomforestclassifier',\n",
       "                 RandomForestClassifier(bootstrap=True, class_weight=None,\n",
       "                                        criterion='gini', max_depth=None,\n",
       "                                        max_features='auto',\n",
       "                                        max_leaf_nodes=None,\n",
       "                                        min_impurity_decrease=0.0,\n",
       "                                        min_impurity_split=None,\n",
       "                                        min_samples_leaf=1, min_samples_split=2,\n",
       "                                        min_weight_fraction_leaf=0.0,\n",
       "                                        n_estimators=10, n_jobs=None,\n",
       "                                        oob_score=False, random_state=None,\n",
       "                                        verbose=0, warm_start=False))],\n",
       "         verbose=False)"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Happy [0.1 0.5 0.3 0.1]\n",
      "Confusion [0.4 0.2 0.3 0.1]\n",
      "Confusion [0.4 0.2 0.  0.4]\n",
      "Happy [0.  0.8 0.  0.2]\n",
      "Happy [0. 1. 0. 0.]\n",
      "Happy [0. 1. 0. 0.]\n",
      "Happy [0. 1. 0. 0.]\n",
      "Happy [0.  0.9 0.  0.1]\n",
      "Happy [0.  0.9 0.  0.1]\n",
      "Happy [0.  0.9 0.  0.1]\n",
      "Happy [0.  0.9 0.  0.1]\n",
      "Happy [0.  0.9 0.  0.1]\n",
      "Happy [0.  0.9 0.  0.1]\n",
      "Happy [0.  0.9 0.  0.1]\n",
      "Happy [0.  0.4 0.2 0.4]\n",
      "Sad [0.  0.  0.9 0.1]\n",
      "Sad [0.  0.  0.9 0.1]\n",
      "Sad [0.  0.  0.9 0.1]\n",
      "Sad [0. 0. 1. 0.]\n",
      "Sad [0. 0. 1. 0.]\n",
      "Sad [0. 0. 1. 0.]\n",
      "Sad [0. 0. 1. 0.]\n",
      "Sad [0.1 0.  0.9 0. ]\n",
      "Sad [0.1 0.  0.9 0. ]\n",
      "Sad [0.1 0.  0.9 0. ]\n",
      "Sad [0.  0.1 0.7 0.2]\n",
      "Victory [0.  0.  0.3 0.7]\n",
      "Victory [0.1 0.1 0.1 0.7]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0.1 0.  0.  0.9]\n",
      "Confusion [0.6 0.  0.  0.4]\n",
      "Confusion [0.7 0.  0.  0.3]\n",
      "Confusion [0.7 0.  0.  0.3]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.7 0.2 0.  0.1]\n",
      "Confusion [0.7 0.2 0.  0.1]\n",
      "Confusion [0.7 0.2 0.  0.1]\n",
      "Confusion [0.7 0.2 0.  0.1]\n",
      "Confusion [0.7 0.2 0.  0.1]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.8 0.1 0.  0.1]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.7 0.2 0.  0.1]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.7 0.2 0.  0.1]\n",
      "Confusion [0.7 0.2 0.  0.1]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Confusion [0.5 0.3 0.  0.2]\n",
      "Happy [0.1 0.5 0.  0.4]\n",
      "Happy [0.1 0.6 0.  0.3]\n",
      "Happy [0.1 0.6 0.  0.3]\n",
      "Happy [0.1 0.6 0.  0.3]\n",
      "Happy [0.2 0.4 0.  0.4]\n",
      "Happy [0.1 0.5 0.  0.4]\n",
      "Confusion [0.6 0.2 0.  0.2]\n",
      "Victory [0.3 0.3 0.  0.4]\n",
      "Confusion [0.5 0.2 0.  0.3]\n",
      "Victory [0.1 0.4 0.  0.5]\n",
      "Victory [0.  0.2 0.  0.8]\n",
      "Victory [0.  0.1 0.  0.9]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0.  0.  0.1 0.9]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0.  0.  0.1 0.9]\n",
      "Victory [0.  0.  0.1 0.9]\n",
      "Victory [0.  0.  0.2 0.8]\n",
      "Victory [0.  0.  0.2 0.8]\n",
      "Victory [0.1 0.  0.4 0.5]\n",
      "Victory [0.1 0.  0.3 0.6]\n",
      "Victory [0.1 0.  0.4 0.5]\n",
      "Sad [0.1 0.  0.6 0.3]\n",
      "Victory [0.1 0.1 0.3 0.5]\n",
      "Victory [0.1 0.  0.2 0.7]\n",
      "Victory [0.2 0.  0.2 0.6]\n",
      "Victory [0.2 0.  0.1 0.7]\n",
      "Victory [0.1 0.1 0.1 0.7]\n",
      "Victory [0.1 0.1 0.1 0.7]\n",
      "Victory [0.1 0.1 0.1 0.7]\n",
      "Victory [0.1 0.1 0.  0.8]\n",
      "Victory [0.2 0.1 0.  0.7]\n",
      "Confusion [0.7 0.1 0.  0.2]\n",
      "Victory [0.1 0.1 0.  0.8]\n",
      "Sad [0.2 0.  0.4 0.4]\n",
      "Sad [0.1 0.  0.6 0.3]\n",
      "Sad [0.  0.  0.6 0.4]\n",
      "Victory [0.  0.  0.1 0.9]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0. 0. 0. 1.]\n",
      "Victory [0.  0.  0.2 0.8]\n",
      "Sad [0.  0.  0.5 0.5]\n",
      "Sad [0.  0.  0.5 0.5]\n",
      "Sad [0.  0.  0.5 0.5]\n",
      "Victory [0.  0.  0.4 0.6]\n",
      "Sad [0.  0.  0.5 0.5]\n",
      "Sad [0.  0.  0.5 0.5]\n",
      "Sad [0.  0.  0.5 0.5]\n",
      "Sad [0.  0.  0.5 0.5]\n",
      "Sad [0.  0.  0.5 0.5]\n",
      "Victory [0.  0.  0.4 0.6]\n",
      "Sad [0.  0.  0.6 0.4]\n",
      "Sad [0.  0.  0.6 0.4]\n"
     ]
    }
   ],
   "source": [
    "cap = cv2.VideoCapture(0)\n",
    "# Initiate holistic model\n",
    "with mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5) as holistic:\n",
    "    \n",
    "    while cap.isOpened():\n",
    "        ret, frame = cap.read()\n",
    "        \n",
    "        # Recolor Feed\n",
    "        image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n",
    "        image.flags.writeable = False        \n",
    "        \n",
    "        # Make Detections\n",
    "        results = holistic.process(image)\n",
    "        # print(results.face_landmarks)\n",
    "        \n",
    "        # face_landmarks, pose_landmarks, left_hand_landmarks, right_hand_landmarks\n",
    "        \n",
    "        # Recolor image back to BGR for rendering\n",
    "        image.flags.writeable = True   \n",
    "        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\n",
    "        \n",
    "        # 1. Draw face landmarks\n",
    "        mp_drawing.draw_landmarks(image, results.face_landmarks, mp_holistic.FACE_CONNECTIONS, \n",
    "                                 mp_drawing.DrawingSpec(color=(80,110,10), thickness=1, circle_radius=1),\n",
    "                                 mp_drawing.DrawingSpec(color=(80,256,121), thickness=1, circle_radius=1)\n",
    "                                 )\n",
    "        \n",
    "        # 2. Right hand\n",
    "        mp_drawing.draw_landmarks(image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS, \n",
    "                                 mp_drawing.DrawingSpec(color=(80,22,10), thickness=2, circle_radius=4),\n",
    "                                 mp_drawing.DrawingSpec(color=(80,44,121), thickness=2, circle_radius=2)\n",
    "                                 )\n",
    "\n",
    "        # 3. Left Hand\n",
    "        mp_drawing.draw_landmarks(image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS, \n",
    "                                 mp_drawing.DrawingSpec(color=(121,22,76), thickness=2, circle_radius=4),\n",
    "                                 mp_drawing.DrawingSpec(color=(121,44,250), thickness=2, circle_radius=2)\n",
    "                                 )\n",
    "\n",
    "        # 4. Pose Detections\n",
    "        mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_holistic.POSE_CONNECTIONS, \n",
    "                                 mp_drawing.DrawingSpec(color=(245,117,66), thickness=2, circle_radius=4),\n",
    "                                 mp_drawing.DrawingSpec(color=(245,66,230), thickness=2, circle_radius=2)\n",
    "                                 )\n",
    "        # Export coordinates\n",
    "        try:\n",
    "            # Extract Pose landmarks\n",
    "            pose = results.pose_landmarks.landmark\n",
    "            pose_row = list(np.array([[landmark.x, landmark.y, landmark.z, landmark.visibility] for landmark in pose]).flatten())\n",
    "            \n",
    "            # Extract Face landmarks\n",
    "            face = results.face_landmarks.landmark\n",
    "            face_row = list(np.array([[landmark.x, landmark.y, landmark.z, landmark.visibility] for landmark in face]).flatten())\n",
    "            \n",
    "            # Concate rows\n",
    "            row = pose_row+face_row\n",
    "            \n",
    "#             # Append class name \n",
    "#             row.insert(0, class_name)\n",
    "            \n",
    "#             # Export to CSV\n",
    "#             with open('coords.csv', mode='a', newline='') as f:\n",
    "#                 csv_writer = csv.writer(f, delimiter=',', quotechar='\"', quoting=csv.QUOTE_MINIMAL)\n",
    "#                 csv_writer.writerow(row) \n",
    "\n",
    "            # Make Detections\n",
    "            X = pd.DataFrame([row])\n",
    "            body_language_class = model.predict(X)[0]\n",
    "            body_language_prob = model.predict_proba(X)[0]\n",
    "            print(body_language_class, body_language_prob)\n",
    "            \n",
    "            # Grab ear coords\n",
    "            coords = tuple(np.multiply(\n",
    "                            np.array(\n",
    "                                (results.pose_landmarks.landmark[mp_holistic.PoseLandmark.LEFT_EAR].x, \n",
    "                                 results.pose_landmarks.landmark[mp_holistic.PoseLandmark.LEFT_EAR].y))\n",
    "                        , [640,480]).astype(int))\n",
    "            \n",
    "            cv2.rectangle(image, \n",
    "                          (coords[0], coords[1]+5), \n",
    "                          (coords[0]+len(body_language_class)*20, coords[1]-30), \n",
    "                          (245, 117, 16), -1)\n",
    "            cv2.putText(image, body_language_class, coords, \n",
    "                        cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)\n",
    "            \n",
    "            # Get status box\n",
    "            cv2.rectangle(image, (0,0), (250, 60), (245, 117, 16), -1)\n",
    "            \n",
    "            # Display Class\n",
    "            cv2.putText(image, 'CLASS'\n",
    "                        , (95,12), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1, cv2.LINE_AA)\n",
    "            cv2.putText(image, body_language_class.split(' ')[0]\n",
    "                        , (90,40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)\n",
    "            \n",
    "            # Display Probability\n",
    "            cv2.putText(image, 'PROB'\n",
    "                        , (15,12), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1, cv2.LINE_AA)\n",
    "            cv2.putText(image, str(round(body_language_prob[np.argmax(body_language_prob)],2))\n",
    "                        , (10,40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)\n",
    "            \n",
    "        except:\n",
    "            pass\n",
    "                        \n",
    "        cv2.imshow('Raw Webcam Feed', image)\n",
    "\n",
    "        if cv2.waitKey(10) & 0xFF == ord('q'):\n",
    "            break\n",
    "\n",
    "cap.release()\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tuple(np.multiply(np.array((results.pose_landmarks.landmark[mp_holistic.PoseLandmark.LEFT_EAR].x, \n",
    "results.pose_landmarks.landmark[mp_holistic.PoseLandmark.LEFT_EAR].y)), [640,480]).astype(int))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
